﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace zyc
{
    /// <summary>
    /// 释放模式（文件，网络连接，套接字，互斥体..等非托管资源）
    /// </summary>
   public class Destructor:IDisposable
    {
       //表示是否已经释放
       private bool disposed = false;
       /// <summary>
       /// 手动方法1
       /// </summary>
       public void Dispose()
       {
           Dispose(true);
       }
       /// <summary>
       /// 手动方法2
       /// </summary>
       public void Close()
       {
           Dispose(true);
       }
       /// <summary>
       /// 析构 终结器
       /// </summary>
       ~Destructor()
       {
           Dispose(false);
       }
       /// <summary>
       /// 释放核心逻辑
       /// </summary>
       /// <param name="disposing">是否手动释放</param>
       public void Dispose(bool disposing)
       {
           if (!disposed)
           {
               if (disposing)
               {
                   
                   Console.WriteLine("调用所引用对象的Dispose方法");
                   GC.SuppressFinalize(this);//去除终结队列
               }
               Console.WriteLine("释放类本身所有的非托管资源");
               disposed = true;
           }
       }

    }
}
